/**
 * 资源管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：资源管理页面
     创建日期：2015-11-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	resourceList();
});

/**
 * 查询资源列表
 * @returns
 */
var resourceList = function(){
	
	var type = $('#type').val();
	var name = $('#name').val();
	var level = $('#level').val();
	var parentId = $('#parentId').val();
	var status = $('#status').val();
	
	if( level == 3 )
	{
		type = 2;
		$('#type').val( type );
	}
	
	var datas = {"p":"authorityMenuManage","m":"findResourceListPaged","type":type,"name":name,"level":level,"parentId":parentId,"status":status};//请求参数
	var columnDefine = {"id":"ID","name":"名称","type":"类型","indexNo":"排序","sn":"编号","link":"链接","level":"级别","status":"状态"};//表格栏目定义
	var formatters = {//自定义表格格式
			 "status": function (column, row)
	            {
	            	var txt ='';
	            	switch (row.status) {
					case 1:
						txt='可用';
						break;
					case 2:
						txt='不可用';
						break;
					default:
						break;
					}
	                return  txt;
	            },
				"type": function (column, row)
				{
					var txt ='';
					switch (row.type) {
					case 1:
						txt='菜单';
						break;
					case 2:
						txt='按钮';
						break;
					default:
						break;
					}
					return  txt;
				}
	};
	var buttons =''; //操作按钮
	buttons+='<p>';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showModifyWin();">编辑</button>&nbsp;&nbsp;';
	if( type == 1 && level < 3 )
	{
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="showChildren();">查看下级</button>&nbsp;&nbsp;';
	}	
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="deleteResource();">删除</button>';
	buttons+='</p>';
	
	hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};


/**
 * 查看子项目
 * @returns
 */
var showChildren = function(){
	var level = $('#level').val();
	level = parseInt(level)+1;
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	window.location.href='page?p=page/resourceManage&parentId='+json.id+'&level='+level;
};


/**
 * 打开新建窗口
 * @returns
 */
var showNewWin = function(){
	var type = $('#type').val();
	type = ( type ==null || type==''?1:type);
	$('#newPop input').val('');
	$('#newPop .type').val(type);
	$('#newPop textarea').val('');
	$('#newPop').modal('show');
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(obj){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#editPop .id').val(json.id);
	$('#editPop .name').val(json.name);
	$('#editPop .indexNo').val(json.indexNo);
	$('#editPop .sn').val(json.sn);
	$('#editPop .type').val(json.type);
	$('#editPop .description').val(json.description);
	$('#editPop .link').val(json.link);
	$('#pop_win').modal('hide');
	$('#editPop').modal('show');
};

/**
 * 新建资源
 * @returns
 */
var newResource = function(){

	var parentId = $('#parentId').val();
	var level = $('#level').val();
	var name = $('#newPop .name').val();
	var indexNo = $('#newPop .indexNo').val();
	
	var sn = $('#newPop .sn').val();
	var type = $('#newPop .type').val();
	var description = $('#newPop .description').val();
	var link = $('#newPop .link').val();
	
	var datas = {"p":"authorityMenuManage","m":"addResource","name":name,"type":type,"parentId":parentId,"level":level,"indexNo":indexNo,"sn":sn,"description":description,"link":link};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		resourceList();
		$('#newPop').modal('hide');
		common.showMsg('info', '新建成功', '新建成功!');
	}
	else
	{
		common.showMsg('error', '创建失败', json.message);
	}	
};

/**
 * 编辑分组
 * @returns
 */
var modifyResource = function(){
	
	var id = $('#editPop .id').val();
	var name = $('#editPop .name').val();
	var indexNo = $('#editPop .indexNo').val();
	var sn = $('#editPop .sn').val();
	var type = $('#editPop .type').val();
	var description = $('#editPop .description').val();
	var link = $('#editPop .link').val();
	
	var datas = {"p":"authorityMenuManage","m":"modifyResource","name":name,"type":type,"description":description,"id":id,"indexNo":indexNo,"sn":sn,"link":link};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		resourceList();
		$('#editPop').modal('hide');
		common.showMsg('info', '', '修改成功');
	}
	else
	{
		common.showMsg('error', '修改成功', json.message);
	}		
};


/**
 * 删除资源
 * @returns
 */
var deleteResource = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	
	var datas = {"p":"authorityMenuManage","m":"deleteResource","id":json.id};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		resourceList();
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '删除成功');
	}
	else
	{
		common.showMsg('error', '删除成功', json.message);
	}		
};
